package com.Adapters;


import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import com.Adapters.Items.ItemPost;
import com.Adapters.Items.ItemPostPicture;
import com.Adapters.Items.RowType;
import com.Data.Post;
import com.Init.ImageDownloaderInit;
import com.Managers.FragmentManagerClass;
import com.SoapClasses.SoapUtil;
import com.activities.DefaultFunctions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.utils.AppConstant;


public class PostAdapter extends ArrayAdapter<Post> implements OnScrollListener {

    public static int COMMENT_LESS = -50;

    private static int postCount = 0;

    private DefaultFunctions actualMain;

    private ImageLoader loader;

    private boolean loading = true;

    private int previousTotal = 0;

    private ArrayList<ItemPost> rows;

    private String tagFilter;

    public String[] compilePaths() {

        String[] pst = new String[rows.size()];

        for (int i = 0; i < rows.size(); i++) {
            ItemPost p = rows.get(i);
            if (p.getViewType() == RowType.PICTURE_POST.ordinal())
                pst[i] = ((ItemPostPicture) p).getPicture().getPathFull();

        }

        return pst;
    }

    public int[] compileIds() {

        int[] pst = new int[rows.size()];

        for (int i = 0; i < rows.size(); i++) {
            ItemPost p = rows.get(i);
            if (p.getViewType() == RowType.PICTURE_POST.ordinal())
                pst[i] = ((ItemPostPicture) p).getPicture().getID();

        }

        return pst;
    }

    public PostAdapter(DefaultFunctions actualMain, Context context,
                int resource) {

        super(context, resource);

        this.actualMain = actualMain;
        rows = new ArrayList<ItemPost>();
        FragmentManagerClass.adapterPosts = this;
        setOptions();

    }

    private void setOptions() {

        ImageDownloaderInit.config(getContext(), "");
        this.loader = ImageLoader.getInstance();

    }

    @Override
    public void add(Post object) {

        rows.add(new ItemPostPicture(LayoutInflater.from(getContext()), object, actualMain, loader));

        if (postCount == AppConstant.POST_BEFORE_AD)
            addAdv();

        postCount++;
    }

    public void addAdv() {

        // rows.add(new ItemPostADV(LayoutInflater.from(getContext()),
        // actualMain));
        postCount = 0;
    }

    public void addAt(Post object, int position) {

        rows.add(position, new ItemPostPicture(LayoutInflater.from(getContext()), object, actualMain, loader));

    }

    @Override
    public void clear() {

        rows.clear();

    }

    public void clearAnimationList() {

        if (rows.size() > 0)
            rows.get(0).clearAnimatonList();
    }

    @Override
    public int getCount() {

        return rows.size();
    }

    @Override
    public long getItemId(int position) {

        return rows.get(position).getPostId();
    }

    @Override
    public int getItemViewType(int position) {

        return rows.get(position).getViewType();
    }

    public void getMorePosts() {

        actualMain.loadFrom(SoapUtil.MENU_VIEW, getCount());
    }

    public ItemPost getPost(int position) {

        return rows.get(position);
    }

    public String getTagFilter() {

        return this.tagFilter;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

        return rows.get(position).getView(v, position);
    }

    // /////////////////////////////////////////////////
    // CLASS METHODS
    // ///////////////////////////////////////////////

    @Override
    public int getViewTypeCount() {

        return RowType.values().length;
    }

    public boolean isLoading() {

        return loading;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if (loading) {
            // if trying to loading

            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        // si se cargaron los items necesarios
        if (!(loading)
                    && (totalItemCount - visibleItemCount) <= (firstVisibleItem)) {

            if (getCount() > 1) {
                getMorePosts();
            }
            //
            loading = true;
        }
    }

    // /////////////////////////////////////////////////
    // LIST METHODS
    // ///////////////////////////////////////////////

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        if (loader != null) {
            switch (scrollState) {
                case OnScrollListener.SCROLL_STATE_IDLE:
                    loader.resume();
                    break;
                case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    loader.pause();
                    break;
                case OnScrollListener.SCROLL_STATE_FLING:
                    loader.pause();
                    break;
            }
        }
    }

    @Override
    public void remove(Post object) {

        for (int i = 0; i < rows.size(); i++) {
            ItemPost p = rows.get(i);
            if (object.getID() == p.getPostId()) {
                int next = (i + 1);
                if (next < getCount())
                    if (rows.get(next).getViewType() == RowType.ADV.ordinal())
                        rows.remove(next);

                rows.remove(i);
                notifyDataSetChanged();

                break;
            }
        }

    }

    public void setLoader(ImageLoader loader) {

        this.loader = loader;
    }

}
